package com.fingard.dsp.bank.directbank.klb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsHisBalDetail;
import com.fingard.io.FileHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class KLB01Balance extends KLB01Base implements IBalance {

	@Override
	public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
		// TODO Auto-generated method stub
		// 余额查询返回ATS的对象
		RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
		RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();

		retAts.writeStart();// 写返回ATS的统一头部信息
		try {

			bnkVersion = "0.0.0.1";
			bank = "KLB";
			bnkOrgId=bankFront.corpNo;
			bnkoOrgName=bankFront.org;
			bnkTxCode = "1025";
			bnkTxPackNo = p_req7506.reqHeader.transSeqID;
			XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
			writeCommonHeader(xmlWriter);

			while (p_req7506.readRecord()) {
				xmlWriter.writeStartElement("body");
				xmlWriter.writeElementString("AccNo", p_req7506.readedDetail.actNum);
				xmlWriter.writeElementString("SubAcct", " "); // 款项代码
				xmlWriter.writeEndElement();
				xmlWriter.writeEndDocument();
				 String[] retStrArr = sendToBank(xmlWriter.toXmlString());
//				String[] retStrArr = new String[] { "", "" };
//				retStrArr[1] = FileHelper.readFileText("D:\\m.txt", "GBK");
				if (retStrArr[0].length() == 0) {
					StringBuilder tmpSbMsg = new StringBuilder();
					XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);

					if (tmpBkReader != null) {
						String tmpRespCode = tmpBkReader.getFirstText("/root/head/RetCode");
						String tmpRespInfo = tmpBkReader.getFirstText("/root/head/RetMsg");
						if ("M0001".equals(tmpRespCode) || "000000".equals(tmpRespCode)) {
							while (tmpBkReader.read("/root/body")) {
								tmpRetDetail.respCode = "0";
								tmpRetDetail.respInfo = tmpRespInfo;
								 tmpRetDetail.actArea = p_req7506.readedDetail.actArea;
								 tmpRetDetail.actCur = p_req7506.readedDetail.actCur;
								 tmpRetDetail.actNum = p_req7506.readedDetail.actNum;
								tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("Ccy"));// 币种
								tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("Bal1");// 账面余额
								tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("Bal2");//
								tmpRetDetail.frozenBal = tmpBkReader.readedXElement.getSingleText("Bal3");
								tmpRetDetail.yesterdayBal = tmpBkReader.readedXElement.getSingleText("Bal4");
								retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
							}
						} else {

						}

						// String tmpRespCode =
						// tmpBkReader.readedXElement.getSingleText("/root/head/RetCode");
						// String tmpRespInfo =
						// tmpBkReader.readedXElement.getSingleText("/root/head/RetMsg");

					} else {

						retAts.writeRespCode("-1");
						retAts.writeRespInfo(tmpSbMsg.toString());
					}
				} else {
					retAts.writeRespCode("-1");
					retAts.writeRespInfo(retStrArr[1]);
				}
			}

		} catch (Exception e) {
			retAts.writeRespCode("-1");
			retAts.writeRespInfo(e.getMessage());
			e.printStackTrace();
			WriteBankLogLn(e);
		}
		retAts.writeEnd();
		return retAts;
	}

	@Override
	public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
		// TODO Auto-generated method stub
		RetAts7505HisBalQry retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
		String tmpRespCode = "";
		String tmpRespInfo = "";
		retAts.writeStart();// 写返回ATS的统一头部信息
		try {
			bnkVersion = "0.0.0.1";
			bank = "KLB";
			bnkOrgId = bankFront.org;
			bnkoOrgName = "5000001";
			bnkTxCode = "1025"; // 交易代码
			bnkTxPackNo = p_req7505.reqHeader.transSeqID;

			XmlTextWriter xmlWriter = new XmlTextWriter();
			xmlWriter.autoFormat = false;
			writeCommonHeader(xmlWriter);
			xmlWriter.writeStartElement("body");
			xmlWriter.writeElementString("AccNo", p_req7505.actNum);// 账号
			xmlWriter.writeElementString("SubAcct", p_req7505.actCur); // 币种

			// 发银行
			xmlWriter.writeEndElement();
			xmlWriter.writeEndDocument();
			String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString());
			if (tmpRetBkStr[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
				if (tmpBkReader != null) {
					String tmpRetCode = tmpBkReader.getFirstText("/root/head/RetCode");
					String tmpRetMsg = tmpBkReader.getFirstText("/root/head/RetMsg");

					while (tmpBkReader.read("/root")) {
						RetAtsHisBalDetail tmpRetDetail = new RetAtsHisBalDetail();
						String tmpETYDAT = tmpBkReader.readedXElement.getSingleText("/head/TxDate");// 交易日期
						if (tmpETYDAT.length() == 8) {
							tmpRetDetail.balDate = tmpETYDAT.substring(0, 4) + "-" + tmpETYDAT.substring(4, 6) + "-"
									+ tmpETYDAT.substring(6, 8);
						} else {
							tmpRetDetail.balDate = tmpETYDAT;
						}
						tmpRetDetail.actBal = p_req7505.actArea; // 账户区域
						retAts.writeRetDetail(tmpRetDetail);
					}
				} else {
					tmpRespCode = "-1";
					tmpRespInfo = tmpSbMsg.toString();
				}
			} else {
				tmpRespCode = "-1";
				tmpRespInfo = tmpRetBkStr[1];
			}

		} catch (Exception e) {
			tmpRespCode = "-1";
			tmpRespInfo = e.getMessage();
			e.printStackTrace();
			WriteBankLogLn(e);
		}
		if (tmpRespCode.equals("-1")) {
			retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
			retAts.writeStart();// 写返回ATS的统一头部信息
			retAts.writeRespCode(tmpRespCode);
			retAts.writeRespInfo(tmpRespInfo);
		} else if (retAts.getWrittenRdCount() == 0) {
			retAts.writeRespCode("0");
			retAts.writeRespInfo("返回结果为空，所查询时间段内不存在历史余额");
		}
		retAts.writeEnd();
		return retAts;
	}

}
